What is media-typer?
The media-typer npm package is a simple utility for parsing and formatting media types as per RFC 6838. It allows you to parse media type strings, extract their components, and format an object representing a media type back into a string. It is useful for handling and validating Content-Type and Accept HTTP headers.
What are media-typer's main functionalities?
Parsing media types
This feature allows you to parse a media type string into its constituent parts, such as type, subtype, and parameters. The example code demonstrates how to parse the media type 'application/json'.
const mediaTyper = require('media-typer');
const parsed = mediaTyper.parse('application/json');
console.log(parsed);
Formatting media types
This feature enables you to format an object representing a media type back into a string. The example code shows how to format an object with type 'image' and subtype 'png' into the string 'image/png'.
const mediaTyper = require('media-typer');
const formatted = mediaTyper.format({ type: 'image', subtype: 'png' });
console.log(formatted);
Validating media type names
This feature is used to validate media type strings and will throw an error if the media type is invalid. The example code attempts to parse an invalid media type and catches the error.
const mediaTyper = require('media-typer');
try {
mediaTyper.parse('invalid/type');
} catch (e) {
console.error('Invalid media type', e.message);
}
Other packages similar to media-typer
content-type
The 'content-type' package is similar to media-typer as it provides utilities for parsing and formatting Content-Type header strings. It differs in that it focuses specifically on the Content-Type header and provides more functionality around charset handling and parameter parsing.
mime-types
The 'mime-types' package is another similar package that allows for looking up the content-type associated with a file extension and vice versa. It provides a larger database of MIME types but does not focus on parsing and formatting of media type strings as media-typer does.
negotiator
The 'negotiator' package is used for content negotiation in HTTP transactions. It can parse Accept headers and determine the best match for a response. While it deals with media types, its primary focus is on the negotiation process rather than just parsing and formatting media types.
media-typer
Simple RFC 6838 media type parser
Installation
$ npm install media-typer
API
var typer = require('media-typer')
typer.parse(string)
var obj = typer.parse('image/svg+xml; charset=utf-8')
Parse a media type string. This will return an object with the following
properties (examples are shown for the string 'image/svg+xml; charset=utf-8'
):
-
type
: The type of the media type (always lower case). Example: 'image'
-
subtype
: The subtype of the media type (always lower case). Example: 'svg'
-
suffix
: The suffix of the media type (always lower case). Example: 'xml'
-
parameters
: An object of the parameters in the media type (name of parameter always lower case). Example: {charset: 'utf-8'}
typer.parse(req)
var obj = typer.parse(req)
Parse the content-type
header from the given req
. Short-cut for
typer.parse(req.headers['content-type'])
.
typer.parse(res)
var obj = typer.parse(res)
Parse the content-type
header set on the given res
. Short-cut for
typer.parse(res.getHeader('content-type'))
.
typer.format(obj)
var obj = typer.format({type: 'image', subtype: 'svg', suffix: 'xml'})
Format an object into a media type string. This will return a string of the
mime type for the given object. For the properties of the object, see the
documentation for typer.parse(string)
.
License
MIT